package com.pinyoumei.app.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.pinyoumei.app.R;


public class ShareDialog extends Dialog implements View.OnClickListener{
    private Context context;
    private int layoutResID;//布局文件id
    private int[] listenedItems;//存放控件id的数组
    private LinearLayout linearLayout_weixinhaoyou,linearLayout_pengyouquan;//微信好友，朋友圈
    public ShareDialog(Context context){
        super(context);
        this.context = context;
    }
    public ShareDialog(Context context, int themeResId){
        super(context,themeResId);
        this.context = context;
    }
    public ShareDialog(Context context, boolean cancelable, OnCancelListener cancelListener){
        super(context,cancelable,cancelListener);
    }
    public ShareDialog(Context context, int layoutResID, int[] listenedItems){
        super(context, R.style.dialogStyle);//这个决定dialog的样式
        this.context = context;
        this.layoutResID = layoutResID;
        this.listenedItems = listenedItems;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();//获取一个窗口
        window.setGravity(Gravity.BOTTOM);//设置窗口的位置
        window.setWindowAnimations(R.style.bottom_menu_animation);//设置窗口进入时的动画特效
        WindowManager.LayoutParams layoutParams = window.getAttributes();//获取窗口的属性赋值给layoutParams
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();//获取屏幕宽高
        layoutParams.width = displayMetrics.widthPixels;//宽度设置为屏幕宽度的80%
        //layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(layoutParams);//设置窗口的属性
        setCanceledOnTouchOutside(true);
        setContentView(R.layout.item_layout_sharedialog);
        linearLayout_weixinhaoyou = findViewById(R.id.linearLayout_weixinhaoyou);
        linearLayout_weixinhaoyou.setOnClickListener(this);
        linearLayout_pengyouquan = findViewById(R.id.linearLayout_pengyouquan);
        linearLayout_pengyouquan.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.linearLayout_weixinhaoyou:
                listener.onClick(v);
                dismiss();
                break;
            case R.id.linearLayout_pengyouquan:
                listener.onClick(v);
                dismiss();
                break;
        }
    }
    private OnClickListener listener;
    public interface OnClickListener{
        void onClick(View view);
    }
    public void setOnClickListener(OnClickListener listener){
        this.listener = listener;
    }
    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity=Gravity.BOTTOM;
        layoutParams.width= WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height= WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);

    }
}
